<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qmltypereference.qdoc -->
  <title>vector3d QML Basic Type | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td ><a href="qtquick-qmlmodule.html">QML类型</a></td><td >vector3d QML Basic Type</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">vector3d QML Basic Type</h1>
<span class="subtitle"></span>
<!-- $$$vector3d-description -->
<div class="descr"> <a name="details"></a>
<p>a value with x, y, and z attributes.</p>
<p>The <code>vector3d</code> type refers to a value with <code>x</code>, <code>y</code>, and <code>z</code> attributes.</p>
<p>To create a <code>vector3d</code> value, specify it as a &quot;x,y,z&quot; string:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-rotation.html">Rotation</a></span> { <span class="name">angle</span>: <span class="number">60</span>; <span class="name">axis</span>: <span class="string">&quot;0,1,0&quot;</span> }

</pre>
<p>or with the <a href="../qtqml/qml-qtqml-qt.html#vector3d-method">Qt.vector3d()</a> function:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-rotation.html">Rotation</a></span> { <span class="name">angle</span>: <span class="number">60</span>; <span class="name">axis</span>: <span class="name">Qt</span>.<span class="name">vector3d</span>(<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>) }

</pre>
<p>or as separate <code>x</code>, <code>y</code>, and <code>z</code> components:</p>
<pre class="qml">

  <span class="type"><a href="qml-qtquick-rotation.html">Rotation</a></span> { <span class="name">angle</span>: <span class="number">60</span>; <span class="name">axis</span>.x: <span class="number">0</span>; <span class="name">axis</span>.y: <span class="number">1</span>; <span class="name">axis</span>.z: <span class="number">0</span> }

</pre>
<p>Each attribute of a vector3d value is stored internally as a single-precision floating point number (<code>float</code>).</p>
<p>When integrating with C++, note that any <a href="../qtgui/qvector3d.html">QVector3D</a> value <a href="../qtqml/qtqml-cppintegration-data.html">passed into QML from C++</a> is automatically converted into a <code>vector3d</code> value, and vice-versa.</p>
<p>The vector3d type has the following idempotent functions which can be invoked in QML:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Function Signature</th><th >Description</th><th >Example</th></tr></thead>
<tr valign="top" class="odd"><td >vector3d crossProduct(vector3d other)</td><td >Returns the vector3d result of the cross product of <code>this</code> vector3d with the <code>other</code> vector3d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">,</span><span class="number">6</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>crossProduct(b);
  console<span class="operator">.</span>log(c<span class="operator">.</span>toString()); <span class="comment">// QVector3D(-3, 6, -3)</span>

</pre>
</td></tr>
<tr valign="top" class="even"><td >real dotProduct(vector3d other)</td><td >Returns the scalar real result of the dot product of <code>this</code> vector3d with the <code>other</code> vector3d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">,</span><span class="number">6</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>dotProduct(b);
  console<span class="operator">.</span>log(c); <span class="comment">// 32</span>

</pre>
</td></tr>
<tr valign="top" class="odd"><td >vector3d times(matrix4x4 matrix)</td><td >Returns the vector3d result of transforming <code>this</code> vector3d with the 4x4 <code>matrix</code> with the matrix applied post-vector</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>matrix4x4(<span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">,</span><span class="number">6</span><span class="operator">,</span><span class="number">7</span><span class="operator">,</span><span class="number">8</span><span class="operator">,</span><span class="number">9</span><span class="operator">,</span><span class="number">10</span><span class="operator">,</span><span class="number">11</span><span class="operator">,</span>
                       <span class="number">12</span><span class="operator">,</span><span class="number">13</span><span class="operator">,</span><span class="number">14</span><span class="operator">,</span><span class="number">15</span><span class="operator">,</span><span class="number">16</span><span class="operator">,</span><span class="number">17</span><span class="operator">,</span><span class="number">18</span><span class="operator">,</span><span class="number">19</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>times(b);
  console<span class="operator">.</span>log(c<span class="operator">.</span>toString());
  <span class="comment">// QVector3D(0.774194, 0.849462, 0.924731)</span>

</pre>
</td></tr>
<tr valign="top" class="even"><td >vector3d times(vector3d other)</td><td >Returns the vector3d result of multiplying <code>this</code> vector3d with the <code>other</code> vector3d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">,</span><span class="number">6</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>times(b);
  console<span class="operator">.</span>log(c<span class="operator">.</span>toString()); <span class="comment">// QVector3D(4, 10, 18)</span>

</pre>
</td></tr>
<tr valign="top" class="odd"><td >vector3d times(real factor)</td><td >Returns the vector3d result of multiplying <code>this</code> vector3d with the scalar <code>factor</code></td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="number">4.48</span>;
  var c <span class="operator">=</span> a<span class="operator">.</span>times(b);
  console<span class="operator">.</span>log(c<span class="operator">.</span>toString()); <span class="comment">// QVector3D(4.48, 8.96, 13.44)</span>

</pre>
</td></tr>
<tr valign="top" class="even"><td >vector3d plus(vector3d other)</td><td >Returns the vector3d result of the addition of <code>this</code> vector3d with the <code>other</code> vector3d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">,</span><span class="number">6</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>plus(b);
  console<span class="operator">.</span>log(c<span class="operator">.</span>toString()); <span class="comment">// QVector3D(5, 7, 9)</span>

</pre>
</td></tr>
<tr valign="top" class="odd"><td >vector3d minus(vector3d other)</td><td >Returns the vector3d result of the subtraction of <code>other</code> vector3d from <code>this</code> vector3d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">,</span><span class="number">6</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>minus(b);
  console<span class="operator">.</span>log(c<span class="operator">.</span>toString()); <span class="comment">// QVector3D(-3, -3, -3)</span>

</pre>
</td></tr>
<tr valign="top" class="even"><td >vector3d normalized()</td><td >Returns the normalized form of <code>this</code> vector</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> a<span class="operator">.</span>normalized();
  console<span class="operator">.</span>log(b<span class="operator">.</span>toString());
  <span class="comment">// QVector3D(0.267261, 0.534522, 0.801784)</span>

</pre>
</td></tr>
<tr valign="top" class="odd"><td >real <a href="../qtwidgets/stylesheet-reference.html#length">length()</a></td><td >Returns the scalar real value of the length of <code>this</code> vector3d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> a<span class="operator">.</span>length();
  console<span class="operator">.</span>log(b<span class="operator">.</span>toString()); <span class="comment">// 3.7416573867739413</span>

</pre>
</td></tr>
<tr valign="top" class="even"><td >vector2d toVector2d()</td><td >Returns the vector2d result of converting <code>this</code> vector3d to a vector2d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> a<span class="operator">.</span>toVector2d();
  console<span class="operator">.</span>log(b<span class="operator">.</span>toString()); <span class="comment">// QVector2D(1, 2)</span>

</pre>
</td></tr>
<tr valign="top" class="odd"><td >vector4d toVector4d()</td><td >Returns the vector4d result of converting <code>this</code> vector3d to a vector4d</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> a<span class="operator">.</span>toVector4d();
  console<span class="operator">.</span>log(b<span class="operator">.</span>toString()); <span class="comment">// QVector4D(1, 2, 3, 0)</span>

</pre>
</td></tr>
<tr valign="top" class="even"><td >bool fuzzyEquals(vector3d other, real epsilon)</td><td >Returns true if <code>this</code> vector3d is approximately equal to the <code>other</code> vector3d. The approximation will be true if each attribute of <code>this</code> is within <code>epsilon</code> of <code>other</code>. Note that <code>epsilon</code> is an optional argument, the default <code>epsilon</code> is 0.00001.</td><td ><pre class="cpp">

  var a <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1</span><span class="operator">,</span><span class="number">2</span><span class="operator">,</span><span class="number">3</span>);
  var b <span class="operator">=</span> <span class="type"><a href="../qtqml/qml-qtqml-qt.html">Qt</a></span><span class="operator">.</span>vector3d(<span class="number">1.0001</span><span class="operator">,</span> <span class="number">1.9998</span><span class="operator">,</span> <span class="number">2.0001</span>);
  var c <span class="operator">=</span> a<span class="operator">.</span>fuzzyEquals(b);        <span class="comment">// default epsilon</span>
  var d <span class="operator">=</span> a<span class="operator">.</span>fuzzyEquals(b<span class="operator">,</span> <span class="number">0.005</span>); <span class="comment">// supplied epsilon</span>
  console<span class="operator">.</span>log(c <span class="operator">+</span> <span class="string">&quot; &quot;</span> <span class="operator">+</span> d); <span class="comment">// false true</span>

</pre>
</td></tr>
</table></div>
<p>This basic type is provided by the <a href="qtquick-module.html">QtQuick</a> import.</p>
</div>
<p><b>请参考 </b><a href="../qtqml/qtqml-typesystem-basictypes.html">QML Basic Types</a>。</p>
<!-- @@@vector3d -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
